# Maintainer: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>

_realname=sip
pkgbase=mingw-w64-${_realname}5
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}5")
conflicts=("${MINGW_PACKAGE_PREFIX}-sip")
replaces=("${MINGW_PACKAGE_PREFIX}-sip")
pkgver=5.3.0
pkgrel=6
pkgdesc="A tool that makes it easy to create Python bindings for C and C++ libraries (mingw-w64)"
arch=('any')
license=('custom:"sip"')
url="https://riverbankcomputing.com/software/sip"
depends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python-toml"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
source=(https://pypi.python.org/packages/source/s/sip/sip-${pkgver}.tar.gz
        001-mingw-python.patch
        002-wrappers.patch)
sha256sums=('03a44e20b252ef03ca2891e9439d238af3fd8245f65cdcff238a843d4f455b80'
            '3cbe4be5e37c580d657e7f532a1c3f9ff465513ccd6ce495cba505255bf465ca'
            '2e46495e5df4eed5384a01db93e75f82e37549a4776ca03d07dfef7801d01d78')
install=${_realname}5-${CARCH}.install

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # Apply patches
  patch -p1 -i "${srcdir}"/001-mingw-python.patch
  patch -p1 -i "${srcdir}"/002-wrappers.patch

  cd "${srcdir}"
  rm -rf python-build-${MINGW_CHOST} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MINGW_CHOST}"
}

build() {
  cd python-build-${MINGW_CHOST}
  
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}"/python-build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done  

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}5/LICENSE"
  install -Dm644 LICENSE-GPL2 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}5/LICENSE-GPL2"
  install -Dm644 LICENSE-GPL3 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}5/LICENSE-GPL3"
}
